package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 好友信息变动回调
 * <p>
 * 1、好友的信息（昵称、性别等）修改时；
 * <p>
 * 2、微信用户A是机器人单边好友，机器人开启了自动通过好友请求开关，且微信用户A添加机器人为好友成功后；
 * <p>
 * 3、微信用户A与机器人成为了好友，但微信用户A单方面删除了机器人，机器人未删除微信用户A，然后微信用户A再次添加机器人成功后（前提是微信用户A删除机器人后修改过个人信息）；
 * 以上3种情况任意一种都会触发好友信息变动回调
 *
 * <pre>
 * {
 *     "vcMerchantNo": "202309130012311",
 *     "vcRobotWxId": "wxid_3vz5z7mg256322",
 *     "vcRobotSerialNo": "12EFAE6D4A73E79B08C9C1FE260F9826",
 *     "vcSerialNo": "20250113171501853559912689616",
 *     "nType": 3015,
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "nTimeStamp": 1736759701856,
 *     "Data": [
 *         {
 *             "vcFriendWxId": "wxid_us9yg5o37ewl22",
 *             "vcFriendSerialNo": "CD1F1B696B003F25A93B16A1066F53F5",
 *             "nSex": 1,
 *             "vcNickName": "轻挽",
 *             "vcBase64NickName": "6L275oy9",
 *             "vcHeadImgUrl": "https://wx.qlogo.cn/mmhead/ver_1/NYBbLrk0Xibiaf4ZJBSsyZdN9aoMXMGpwHsG9ZhfqOjjjLE9Ak6iaccsAaiaZDYUn82EUxHIwZtOCoqtkc7ml2HZHwSUQ3LSMjMblnq5FOmb5CpRUq6OXqMtvTYyv9VWUlc5/96",
 *             "vcRemarkName": "",
 *             "vcBase64RemarkName": "",
 *             "vcCountry": "CN",
 *             "vcProvince": "Shaanxi",
 *             "vcCity": "Xi'an",
 *             "vcTagIds": "",
 *             "vcWxAlias": "QAQ134510017"
 *         }
 *     ]
 * }
 * </pre>
 */
@Slf4j
@Component
public class _3015Handler extends RobotContactHandler implements CallbackHandler {

    @Override
    public String name() {
        return "好友信息变动回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 3015;
    }

    @Override
    public void handle(Callback callback) {
        JSONArray data = (JSONArray) callback.getData();
        for (Object datum : data) {
            saveRobotContact(callback, JSONObject.from(datum), LocalDateTime.now());
        }
    }
}
